package com.cheyunkeji.er.view;

import android.app.Activity;

import java.util.Calendar;

import cn.qqtheme.framework.picker.DatePicker;
import cn.qqtheme.framework.util.ConvertUtils;

/**
 * Created by Michael on 2017/9/5 20:05.
 */

public class DatePickerUtil {

    private static DatePicker longTermPicker, shortTermPicker;
    private static Calendar calendar;
    private static String mTag;

    public static void show(Activity activity, DatePicker.OnYearMonthDayPickListener listener, boolean shouldSetTodayAsRangeEnd, String tag) {

        if (shouldSetTodayAsRangeEnd) {
            if (shortTermPicker == null) {
                shortTermPicker = pickDate(activity, listener ,shouldSetTodayAsRangeEnd );
            }
            shortTermPicker.show();
        } else {

            if (longTermPicker == null) {
                longTermPicker = pickDate(activity, listener , shouldSetTodayAsRangeEnd );
            }
            longTermPicker.show();
        }

        mTag = tag;

    }


    public static String getTag() {
        return mTag;
    }

    public static DatePicker pickDate(Activity context, DatePicker.OnYearMonthDayPickListener listener , boolean shouldSetTodayAsRangeEnd ) {

        calendar = Calendar.getInstance();
        final DatePicker picker = new DatePicker(context);
        picker.setCanceledOnTouchOutside(true);
        picker.setUseWeight(true);
        picker.setTopPadding(ConvertUtils.toPx(context, 10));
        picker.setRangeEnd(calendar.get(Calendar.YEAR) + 20, 1, 11);
//        longTermPicker.setTextColor(Color.parseColor("#ff0000"));
        picker.setRangeStart(calendar.get(Calendar.YEAR) - 20, 8, 29);

        if( shouldSetTodayAsRangeEnd ){
            picker.setRangeEnd(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DATE));
        }else
            picker.setRangeEnd(calendar.get(Calendar.YEAR) + 20 , calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DATE));

        picker.setSelectedItem(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DATE));
        picker.setResetWhileWheel(false);
        picker.setOnDatePickListener(listener);

        picker.setOnWheelListener(new DatePicker.OnWheelListener() {

            @Override
            public void onYearWheeled(int index, String year) {
                picker.setTitleText(year + "-" + picker.getSelectedMonth() + "-" + picker.getSelectedDay());
            }

            @Override
            public void onMonthWheeled(int index, String month) {
                picker.setTitleText(picker.getSelectedYear() + "-" + month + "-" + picker.getSelectedDay());
            }

            @Override
            public void onDayWheeled(int index, String day) {
                picker.setTitleText(picker.getSelectedYear() + "-" + picker.getSelectedMonth() + "-" + day);
            }
        });

        return picker;
    }

    public static void clearPicker() {
        longTermPicker = null ;
        shortTermPicker = null ;
    }

}
